package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.domain.Rating;
import com.example.domain.model.RatingDTO;
import com.example.service.RatingService;
import com.example.mapper.RatingMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
* @author Administrator
* @description 针对表【rating】的数据库操作Service实现
* @createDate 2023-03-20 09:46:20
*/
@Service
public class RatingServiceImpl extends ServiceImpl<RatingMapper, Rating> implements RatingService{

    @Autowired
    private RatingMapper ratingMapper;
    @Override
    public IPage<RatingDTO> getList(String userId, Page<Rating> page) {

        return ratingMapper.selectPage(page,userId);
    }

    @Override
    public Rating getByMovieId(String userId,String movieId) {
        Rating rating = ratingMapper.selectOne(new LambdaQueryWrapper<Rating>().
                eq(Rating::getUserId, userId).
                eq(Rating::getMovieId, movieId));
        return rating;
    }

    @Override
    public boolean save(Rating rating,String userId) {
        rating.setUserId(userId);
        return ratingMapper.insert(rating) > 0;
    }

    @Override
    public boolean update(String userId,Rating rating) {
        Rating rating1 = ratingMapper.selectOne(
                new LambdaQueryWrapper<Rating>().
                        eq(Rating::getMovieId,rating.getMovieId()).
                        eq(Rating::getUserId,userId));
        if (rating1 != null){
            String id = rating1.getId();
            rating.setUserId(userId);
            rating.setId(id);
            return ratingMapper.updateById(rating) > 0;
        }else {
            save(rating,userId);
        }
        return false;
    }

    @Override
    public boolean delete(String id) {
        return ratingMapper.deleteById(id) > 0;
    }

    @Override
    public List<Rating> getAll() {
        return ratingMapper.selectList(null);
    }
}




